package org.come.login;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import org.come.bean.UserTable;
import org.come.socket.Agreement;
import org.come.socket.SendMessageUntil;
import org.come.test.Main;
import org.come.until.GsonUtil;
import org.come.until.Music;
import org.come.view.View;

public class RegisterView extends View {

	private SpriteBtn btnBackground,// 注册面板背景
			btnRegister;// 立即注册
	private JTextField textAcc,// 账号
			textScode,// 安全码
//			textVcode,// 验证码
//			textPhone,// 手机号
			texttuiji;// 推荐码
	private JPasswordField textPwd,// 密码
			textPwdA;// 再次输入密码
	// private ValidCode code;// 放置验证码图片
//	private CodeBtn btnCode;// 发送验证码
	private JLabel labMsgTip;// 放置提示信息
//	private boolean againCode = false;// 是否能再次发送
//	private String phoneNum;// 发送的验证码手机号
//	private String CodeNum;// 收到的验证码

	public RegisterView(LoginJpanel loginJpanel) {
		super(-1);
		this.setPreferredSize(new Dimension(424, 406));
		this.setLayout(null);
		// 背景图片
		btnBackground = new SpriteBtn("resource/NewUi/注册界面", -8, -8, false);
		btnBackground.setBounds(0, 0, 424, 406);
		this.add(btnBackground);
		// 立即注册
		btnRegister = new SpriteBtn("resource/NewUi/注册按钮新", 97, 272, false);
//		btnRegister.setBounds(110, 295 + 100, 188, 35);
		btnRegister.setBounds(97, 272, 133, 35);
		btnRegister.addMouseListener(new LoginMouslisten(6, btnRegister, loginJpanel));
		this.add(btnRegister);
		// 放置提示信息
		labMsgTip = new JLabel();
		labMsgTip.setBounds(65, 40, 300, 30);
		labMsgTip.setFont(new Font("楷体", 1, 16));
		labMsgTip.setForeground(Color.red);
		this.add(labMsgTip);
		// 账号
		textAcc = new JTextField();
		textAcc.setBounds(110, 66, 188, 23);
		textAcc.setOpaque(false);
		textAcc.setBorder(BorderFactory.createEmptyBorder());
		textAcc.setForeground(Color.WHITE);
		textAcc.setFont(new Font("宋体", 0, 14));
		textAcc.setText("请输入您的账号");
		textAcc.setCaretColor(Color.WHITE);// 设置光标颜色为白色
		textAcc.getDocument().addDocumentListener(new DocumentListener() {
			@Override
			public void removeUpdate(DocumentEvent e) {
				String str = textAcc.getText();
				if (str.matches("[0-9a-zA-Z]*")) {
					labMsgTip.setText("");
				} else {
					labMsgTip.setText("账号不能包含中文或特殊字符！");
				}
			}

			@Override
			public void insertUpdate(DocumentEvent e) {
				String str = textAcc.getText();
				if (str.matches("[0-9a-zA-Z]*")) {
					labMsgTip.setText("");
				} else {
					labMsgTip.setText("账号不能包含中文或特殊字符！");
				}
			}

			@Override
			public void changedUpdate(DocumentEvent e) {
			}
		});
		textAcc.addKeyListener(new KeyListener() {
			@Override
			public void keyTyped(KeyEvent e) {
			}

			@Override
			public void keyReleased(KeyEvent e) {

				if (textAcc.getText() == null) {
					labMsgTip.setText("请输入您要注册的账号！");
				} else {
					labMsgTip.setText("");
				}
			}

			@Override
			public void keyPressed(KeyEvent e) {
			}
		});
		textAcc.addFocusListener(new FocusListener() {
			@Override
			public void focusLost(FocusEvent e) {
				if (!textAcc.getText().matches("[0-9a-zA-Z]*")) {
					textAcc.setText("");
				}
			}

			@Override
			public void focusGained(FocusEvent e) {
				textAcc.setText("");
			}
		});
		this.add(textAcc);
		// 密码
		textPwd = new JPasswordField();
		textPwd.setBounds(110, 102, 188, 23);
		textPwd.setOpaque(false);
		textPwd.setBorder(BorderFactory.createEmptyBorder());
		textPwd.setForeground(Color.WHITE);
		textPwd.setFont(new Font("宋体", 0, 14));
		textPwd.setCaretColor(Color.WHITE);// 设置光标颜色为白色
		textPwd.setEchoChar('*');// 设置以‘ * ’来显示密码信息
		textPwd.addKeyListener(new KeyListener() {
			@Override
			public void keyTyped(KeyEvent e) {
			}

			@Override
			public void keyReleased(KeyEvent e) {
				if (textPwd.getText() == null) {
					labMsgTip.setText("请输入您要注册的密码！");
				} else {
					labMsgTip.setText("");
				}
			}

			@Override
			public void keyPressed(KeyEvent e) {
			}
		});
		this.add(textPwd);
		// 再次输入密码
		textPwdA = new JPasswordField();
		textPwdA.setBounds(110, 139, 188, 23);
		textPwdA.setOpaque(false);
		textPwdA.setBorder(BorderFactory.createEmptyBorder());
		textPwdA.setForeground(Color.WHITE);
		textPwdA.setFont(new Font("宋体", 0, 14));
		textPwdA.setCaretColor(Color.WHITE);// 设置光标颜色为白色
		textPwdA.setEchoChar('*');// 设置以‘ * ’来显示密码信息
		textPwdA.addKeyListener(new KeyListener() {
			@Override
			public void keyTyped(KeyEvent e) {
			}

			@Override
			public void keyReleased(KeyEvent e) {
				if (textPwdA.getText() != null && textPwd.getText() != null) {
					if (textPwdA.getText().equals(textPwd.getText())) {
						labMsgTip.setText("");
					} else {
						labMsgTip.setText("两次输入的密码不相等！");
					}
				} else if (textPwdA.getText() == null && textPwd.getText() != null) {
					labMsgTip.setText("请再次输入密码！");
				} else if (textPwdA.getText() != null && textPwd.getText() == null) {
					labMsgTip.setText("请输入您要注册的密码！");
				} else {
					labMsgTip.setText("请输入您要注册的密码！");
				}
			}

			@Override
			public void keyPressed(KeyEvent e) {
			}
		});
		this.add(textPwdA);
//		btnCode = new CodeBtn("inkImg/hongmu/2/authCode.png", 1, UIUtils.TEXT_FONT1, "发送验证码", this);
//		btnCode.setBounds(289, 248, 87, 32);
//		btnCode.setForeground(Color.white);
//		this.add(btnCode);
//		// 手机号
//		textPhone = new JTextField();
//		textPhone.setBounds(132, 250, 155, 23);
//		textPhone.setOpaque(false);
//		textPhone.setBorder(BorderFactory.createEmptyBorder());
//		textPhone.setForeground(Color.WHITE);
//		textPhone.setFont(new Font("宋体", 0, 14));
//		textPhone.setCaretColor(Color.WHITE);// 设置光标颜色为白色
//		textPhone.addKeyListener(new KeyListener() {
//			@Override
//			public void keyTyped(KeyEvent e) {
//			}
//
//			@Override
//			public void keyReleased(KeyEvent e) {
//			
//				if(textPhone.getText() != null &&!"".equals(textPhone.getText())){
//					if (phoneNum != null && !textPhone.getText().equals(phoneNum)) {
//						labMsgTip.setText("你输入的手机不匹配");
//					}
//				} else {
//					labMsgTip.setText("请输入手机号！");
//				}
//			}
//
//			@Override
//			public void keyPressed(KeyEvent e) {
//			}
//		});
//		this.add(textPhone);
//		// 验证码
//		textVcode = new JTextField();
//		textVcode.setBounds(132, 288, 90, 23);
//		textVcode.setOpaque(false);
//		textVcode.setBorder(BorderFactory.createEmptyBorder());
//		textVcode.setForeground(Color.WHITE);
//		textVcode.setFont(new Font("宋体", 0, 14));
//		textVcode.setCaretColor(Color.WHITE);// 设置光标颜色为白色
//		textVcode.addKeyListener(new KeyListener() {
//			@Override
//			public void keyTyped(KeyEvent e) {
//			}
//
//			@Override
//			public void keyReleased(KeyEvent e) {
//				if (textVcode.getText() != null && CodeNum != null) {
//					if (textVcode.getText().equalsIgnoreCase(CodeNum)) {
//						labMsgTip.setText("");
//					} else {
//						labMsgTip.setText("你输入的验证码有误！");
//					}
//				} else {
//					labMsgTip.setText("请输入验证码！");
//				}
//			}
//
//			@Override
//			public void keyPressed(KeyEvent e) {
//			}
//		});
//		this.add(textVcode);
		textScode = new JTextField();
		textScode.setBounds(110, 179, 188, 23);
		textScode.setBounds(110, 179, 188, 23);
		textScode.setOpaque(false);
		textScode.setBorder(BorderFactory.createEmptyBorder());
		textScode.setForeground(Color.WHITE);
		textScode.setFont(new Font("宋体", 0, 14));
		textScode.setCaretColor(Color.WHITE);// 设置光标颜色为白色
		textScode.getDocument().addDocumentListener(new DocumentListener() {
			@Override
			public void removeUpdate(DocumentEvent e) {
				String str = textScode.getText();
				if (str.matches("[0-9a-zA-Z]*")) {
					labMsgTip.setText("");
				} else {
					labMsgTip.setText("账号不能包含中文或特殊字符！");
				}
			}

			@Override
			public void insertUpdate(DocumentEvent e) {
				String str = textScode.getText();
				if (str.matches("[0-9a-zA-Z]*")) {
					labMsgTip.setText("");
				} else {
					labMsgTip.setText("安全码不能包含中文或特殊字符！");
				}
			}

			@Override
			public void changedUpdate(DocumentEvent e) {
			}
		});
		textScode.addKeyListener(new KeyListener() {
			@Override
			public void keyTyped(KeyEvent e) {
			}

			@Override
			public void keyReleased(KeyEvent e) {
				if (textScode.getText() == null) {
					labMsgTip.setText("请输入安全码！");
				} else {
					labMsgTip.setText("");
				}
			}

			@Override
			public void keyPressed(KeyEvent e) {
			}
		});
		this.add(textScode);
		// code = new ValidCode();
		// code.setBounds(245, 248, 100, 30);
		// this.add(code);
		texttuiji = new JTextField();
//		texttuiji.setBounds(296, 288, 90, 23);
		texttuiji.setBounds(110, 215, 188, 23);
		texttuiji.setOpaque(false);
		texttuiji.setBorder(BorderFactory.createEmptyBorder());
		texttuiji.setForeground(Color.WHITE);
		texttuiji.setFont(new Font("宋体", 0, 14));
		texttuiji.setCaretColor(Color.WHITE);// 设置光标颜色为白色
		texttuiji.getDocument().addDocumentListener(new DocumentListener() {
			@Override
			public void removeUpdate(DocumentEvent e) {

			}

			@Override
			public void insertUpdate(DocumentEvent e) {

			}

			@Override
			public void changedUpdate(DocumentEvent e) {
			}
		});
		texttuiji.addKeyListener(new KeyListener() {
			@Override
			public void keyTyped(KeyEvent e) {
			}

			@Override
			public void keyReleased(KeyEvent e) {
				if (textScode.getText() == null) {
					labMsgTip.setText("请输入推荐码！");
				} else {
					labMsgTip.setText("");
				}
			}

			@Override
			public void keyPressed(KeyEvent e) {
			}
		});
		this.add(texttuiji);

	}

	public void zc() {
		String acc = textAcc.getText();// 账号
		if ("".equals(acc) || "请输入您的账号".equals(acc) || !acc.matches("[0-9a-zA-Z]*")) {
			labMsgTip.setText("账号不能包含中文或特殊字符！");
			textAcc.setText("");
			return;
		}
		String pwd = textPwd.getText();// 密码
		String pwdA = textPwdA.getText();// 再次的密码
		if ("".equals(pwd) || !pwd.equals(pwdA)) {
			labMsgTip.setText("两次输入的密码不相等！");
			textPwd.setText("");
			return;
		}
		if (pwd.length() <= 1 || pwd.length() > 12) {
			labMsgTip.setText("密码长度需要2-12位字符！");
			return;
		}
		String Aqm = textScode.getText();
		if ("".equals(Aqm)) {
			labMsgTip.setText("请输入安全码!!!");
			return;
		}
		if (Aqm.length() <= 1 || Aqm.length() > 18) {
			labMsgTip.setText("安全码长度需要1-18位字符！");
			return;
		}
//		String phone = textPhone.getText();
//		if (phone != null && !"".equals(phone)) {
////			//system.out.println("phoneNum:" + phoneNum);
//			if(phoneNum == null){
//				labMsgTip.setText("还未发送验证码");
//				return;
//			}
//			if (!phone.equals(phoneNum)) {
//				labMsgTip.setText("手机号不匹配");
//				return;
//			}
//		} else {
//			labMsgTip.setText("手机号不能为空");
//			return;
//		}
		String tuiji = texttuiji.getText();
//		//system.out.println(tuiji);
		if (tuiji != null && !"".equals(tuiji)) {
		} else {
			labMsgTip.setText("推荐码不能为空");
			return;
		}
		RegisterView registerView = Main.frame.getLoginJpanel().getLoginView().getRegisterView();
//		String codeNum2 = registerView.getCodeNum();
//		if(codeNum2 == null){
//			labMsgTip.setText("还未发送验证码");
//			return;
//		}
//		if ("".equals(textVcode.getText()) || !codeNum2.equals(textVcode.getText())) {
//			labMsgTip.setText("您的验证码为空或输入有误！");
//			textVcode.setText("");
//			return;
//		}
//		registerView.setCodeNum(null);
//		textVcode.setText("");
		labMsgTip.setText("");
		// 实例化bean
		UserTable userTable = new UserTable();
		userTable.setUsername(acc);// 账号
		userTable.setUserpwd(pwd);// 密码
		userTable.setSafety(Aqm);// 安全码
//		userTable.setPhonenumber(phone);// 手机号
		userTable.setTuiji(tuiji);// 推荐码

		// 向服务器发送信息
		/** zrikka 2020 0415 */
//        String serverMes = Agreement.getAgreement().Account_RegisterAgreement(GsonUtil.getGsonUtil().getgson().toJson(userTable));
//        SendMessageUntil.loginToServer(serverMes);
		String json = GsonUtil.getGsonUtil().getgson().toJson(userTable);
		String serverMes = Agreement.getAgreement().registerAgreement(json);
		SendMessageUntil.toServer(serverMes);
        /***/
	}

//	private int num = 300;
//	private int i = 0;

	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		btnBackground.draw(g);
		btnRegister.draw(g);
//		if (againCode) {
//			if (i > 10) {
//				i = 0;
//				num--;
//				btnCode.setText(num + "");
//				if (num <= 0) {
//					againCode = false;
//					btnCode.setText("发送验证码");
//					btnCode.setBtn(1);
//					num = 300;
//				}
//			}
//			i++;
//		}
	}

	public JLabel getLabMsgTip() {
		return labMsgTip;
	}

	public void setLabMsgTip(JLabel labMsgTip) {
		this.labMsgTip = labMsgTip;
	}

//	public CodeBtn getBtnCode() {
//		return btnCode;
//	}
//
//	public void setBtnCode(CodeBtn btnCode) {
//		this.btnCode = btnCode;
//	}

//	public boolean isAgainCode() {
//		return againCode;
//	}
//
//	public void setAgainCode(boolean againCode) {
//		this.againCode = againCode;
//	}

//	public JTextField getTextPhone() {
//		return textPhone;
//	}
//
//	public void setTextPhone(JTextField textPhone) {
//		this.textPhone = textPhone;
//	}

//	public String getPhoneNum() {
//		return phoneNum;
//	}
//
//	public void setPhoneNum(String phoneNum) {
//		this.phoneNum = phoneNum;
//	}
//
//	public String getCodeNum() {
//		return CodeNum;
//	}
//
//	public void setCodeNum(String codeNum) {
//		CodeNum = codeNum;
//	}

}
